package view;

import app.App;
import component.CustomJTable;
import model.Product;
import model.StockIn;
import model.StockOut;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.List;

public class WarehousePane {

    private JPanel contentPane;
    private JTable table_product;
    private JButton btn_stockin;
    private JTable table_inHistory;
    private JButton btn_return;
    private JTable table_outHistory;
    private JButton btn_refresh_product;
    private JButton btn_refresh_in;
    private JButton btn_refresh_out;
    private JButton btn_mod;

    DefaultTableModel tableModel_product;
    DefaultTableModel tableModel_inHistory;
    DefaultTableModel tableModel_outHistory;

    private void createUIComponents() {
        table_product = new CustomJTable(new String[]{"商品号", "商品名", "类别", "售价", "进价", "库存数量"});
        tableModel_product = (DefaultTableModel) table_product.getModel();
        notifyProductDataChanged();
        CustomJTable.resizeColumnWidth(table_product);

        table_inHistory = new CustomJTable(new String[]{"入库号","商品号","商品名","进价","数量","供应商","入库时间"});
        tableModel_inHistory = (DefaultTableModel) table_inHistory.getModel();
        notifyInHistoryDataChanged();
        CustomJTable.resizeColumnWidth(table_inHistory);

        table_outHistory = new CustomJTable(new String[]{"出库号","商品号","商品名","售价","进价","数量","备注"});
        tableModel_outHistory = (DefaultTableModel) table_outHistory.getModel();
        notifyOutHistoryDataChanged();
        CustomJTable.resizeColumnWidth(table_outHistory);

    }

    public WarehousePane() {

        //入库按钮（显示入库单）
        btn_stockin.addActionListener(e -> {
            if (App.checkWareHousePermission()) {
                new DialogStockIn(WarehousePane.this).setVisible(true);
            }
            else {
                JOptionPane.showMessageDialog(contentPane, "当前用户没有该权限", "权限不足！", JOptionPane.ERROR_MESSAGE);
            }
        });
        //修改商品信息
        btn_mod.addActionListener(e -> {
            if (App.checkWareHousePermission()) {
                int row = table_product.getSelectedRow();
                if (row != -1) {
                    //获得要修改的旧用户
                    Product oldProduct = App.currentProductList.get(row);
                    new DialogProductMod(WarehousePane.this, oldProduct).setVisible(true);
                }
            }
            else {
                JOptionPane.showMessageDialog(contentPane, "当前用户没有该权限", "权限不足！", JOptionPane.ERROR_MESSAGE);
            }
        });
        //退货按钮
        btn_return.addActionListener(e -> {
            if (App.checkWareHousePermission()) {
                int row = table_inHistory.getSelectedRow();
                if (row != -1) {
                    if (JOptionPane.showConfirmDialog(contentPane, "是否确定退货?(选择多行则只有第一行有效)", "确认退货",
                            JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == 0) {
                        if (StockOut.returnProduct((int) table_inHistory.getValueAt(row, 0))) {
                            JOptionPane.showMessageDialog(contentPane, "退货成功", null, JOptionPane.INFORMATION_MESSAGE);
                            notifyProductDataChanged();
                            notifyInHistoryDataChanged();
                            notifyOutHistoryDataChanged();
                        }
                    }
                }
            }
            else {
                JOptionPane.showMessageDialog(contentPane, "当前用户没有该权限", "权限不足！", JOptionPane.ERROR_MESSAGE);
            }
        });

        btn_refresh_product.addActionListener(e -> notifyProductDataChanged());
        btn_refresh_in.addActionListener(e -> notifyInHistoryDataChanged());
        btn_refresh_out.addActionListener(e -> notifyOutHistoryDataChanged());
    }

    public JPanel getContentPane() {
        return contentPane;
    }

    public void notifyProductDataChanged() {
        System.out.println("库存管理列表更新：");
        App.currentProductList = Product.getProducts();

        tableModel_product.setRowCount(0);
        List<Product> currentProductList = App.currentProductList;
        if (currentProductList != null) {
            for (Product product : currentProductList) {
                tableModel_product.addRow(product.toProductListObjectArray());
            }
        }
    }

    public void notifyInHistoryDataChanged() {
        System.out.println("入库历史列表更新：");
        App.currentStockInList = StockIn.getStockInList();

        tableModel_inHistory.setRowCount(0);
        List<StockIn> currentStockInList = App.currentStockInList;
        if (currentStockInList != null) {
            for (StockIn stockIn : currentStockInList) {
                tableModel_inHistory.addRow(stockIn.toTableObjectArray());
            }
        }
    }

    public void notifyOutHistoryDataChanged() {
        System.out.println("出库历史列表更新：");
        App.currentStockOutList = StockOut.getStockOutList();

        tableModel_outHistory.setRowCount(0);
        List<StockOut> currentStockOutList = App.currentStockOutList;
        if (currentStockOutList != null) {
            for (StockOut stockOut : currentStockOutList) {
                tableModel_outHistory.addRow(stockOut.toTableObjectArray());
            }
        }
    }
}
